package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderAddressDao;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.OrderAddressService;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderAddressService.class)
@Transactional
public class OrderAddressServiceImpl implements OrderAddressService {
    @Autowired
    private OrderAddressDao orderAddressDao;

    @Override
    public Result addOrder(Map map) throws Exception {
        //获取日期
        String orderDate = (String) map.get("orderDate");
        //查询当天有没有进行预约设置
        OrderSetting orderSetting = orderAddressDao.findDate(orderDate);
        if(orderSetting==null){
            //当天没有预约设置,不可以进行预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);

        }

        //可预约人数是否已满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if(reservations>=number){
            //预约人数已满,不可在进行预约
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //获取医院的地址
        String address = (String) map.get("address");
        //判断是否为会员
        String telephone = (String) map.get("telephone");
        Member member = orderAddressDao.findMember(telephone);
        if(member!=null){
            //根据套餐id,会员id以及当天的日期查询当天有没有订单
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
            Integer memberId = member.getId();
            List<Order> orderList = orderAddressDao.findOrder(setmealId,memberId,orderDate);
            if(orderList!=null&orderList.size()>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }


        }else {
            //该用户没有注册,应该注册为会员

           member =  new Member();
           member.setPhoneNumber(telephone);
           member.setRegTime(DateUtils.parseString2Date(orderDate));
           member.setIdCard((String) map.get("idCard"));
           member.setSex((String) map.get("sex"));
           member.setName((String) map.get("name"));
           orderAddressDao.addMember(member);
        }
        //变更可预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderAddressDao.updateOrderSetting(orderSetting);

        //预约成功
        Order order = new Order();
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderAddressDao.addOrder(order);

        //根据地址添加中间表信息
        Address address1 = orderAddressDao.findAddress(address);
        Integer address1Id = address1.getId();
        Integer orderId = order.getId();
        orderAddressDao.addOA(address1Id,orderId);
        return new Result(true,MessageConstant.ADD_ORDER_SUCCESS,order.getId());
    }

    @Override
    public List<Map<String, String>> findAddress() {
        //查询地址列表的数据
        List<String> stringList = orderAddressDao.findAddressAll();
        List<Map<String, String>> result = new ArrayList<>();
        for (int i = 0; i < stringList.size(); i++) {
            Map<String,String> map = new HashMap<>();
            map.put("value",""+i);
            map.put("label",stringList.get(i));
            result.add(map);
        }
        return result;
    }

    @Override
    public Setmeal findById(Integer id) {
        return orderAddressDao.findById(id);
    }
}
